/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ereconquista.dao;

import com.ereconquista.modelo.Publicacion;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * @author Claudio
 */
public class PublicacionDAO extends BaseDAO<Publicacion> implements Serializable {

    public List<Publicacion> getUltimasPublicaciones() {
        
        EntityManager em = null;
        String sQuery = "";
       try {
           sQuery = "SELECT p FROM Publicacion p "
                +" WHERE p.fechaInicio <= :fechaInicio "
                +" AND p.fechaFin >= :fechaFin "
                +" AND (p.estado = 'B' or p.estado = 'C' )" 
                +" ORDER BY p.fechaInicio DESC, p.id DESC ";                       
                        
           em = getEntityManager();           
           Query q = em.createQuery(sQuery);
           
           q.setParameter("fechaInicio", new Date());
           q.setParameter("fechaFin", new Date());           
           
           q.setMaxResults(120);
           
           return q.getResultList();
       } catch (Exception e) {
           
           Logger.getLogger(PublicacionDAO.class.getName()).log(Level.SEVERE, "getUltimasPublicaciones", e);                   
           return new ArrayList<Publicacion>();
           
       }finally {
            if (em != null) {
                em.close();
            }
        }  
      
    }

    public Publicacion getPublicacionByLink(String idPublicacion) {
         
        EntityManager em = null;
        String sQuery = "";
        try {
           sQuery = "SELECT p FROM Publicacion p "
                +" WHERE p.fechaInicio <= :fechaInicio "
                +" AND p.fechaFin >= :fechaFin "                   
                +" AND p.linkActivador = :linkActivador "                      
                +" ORDER BY p.fechaInicio DESC";                       
                        
           em = getEntityManager();           
           Query q = em.createQuery(sQuery);
           
           q.setParameter("fechaInicio", new Date());
           q.setParameter("fechaFin", new Date()); 
           q.setParameter("linkActivador", idPublicacion);
          
           return (Publicacion) q.getSingleResult();
       } catch (Exception e) {
           
           Logger.getLogger(PublicacionDAO.class.getName()).log(Level.SEVERE, "getPublicacionByLink", e);                   
           return null;
       }finally {
            if (em != null) {
                em.close();
            }
        }  
    }

    public List<Publicacion> getPublicacionesByFiltro(Map<String, String> filtro) {
        
        EntityManager em = null;
        try {
            
//            System.err.println("filtro:" + filtro);
            
            filtro.put("fechaInicio <= ", ":fechaInicio");
            filtro.put("fechaFin >= ", ":fechaFin");
            
            String sQuery = "SELECT p FROM Publicacion p ";
            sQuery += generarStringFiltro(filtro,"p", true);            
            //sQuery += " AND p.activo = 'S' ";                       
            sQuery += " AND (p.estado = 'B' or p.estado = 'C' ) " ;
            sQuery += " ORDER BY p.fechaInicio DESC ";
           
            em = getEntityManager(); 
            Query q = getEntityManager().createQuery(sQuery);
            
            q.setParameter("fechaInicio", new Date());
            q.setParameter("fechaFin", new Date()); 

            q.setMaxResults(100);
            
            return q.getResultList();

        } catch (Exception e) {

            Logger.getLogger(PublicacionDAO.class.getName()).log(Level.SEVERE, "getPublicacionesByFiltro", e);                   
            return new ArrayList<Publicacion>();
        }finally {
            if (em != null) {
                em.close();
            }
        }  
    }

    public List<Publicacion> getPublicacionByUsuario(Integer idUsuario) {
        
        EntityManager em = null;
        String sQuery = "";
        try {
           sQuery = "SELECT p FROM Publicacion p "
                +" WHERE p.usuario.id = :idUsuario "                      
                +" ORDER BY p.fechaInicio DESC";                       
                        
           em = getEntityManager();           
           Query q = em.createQuery(sQuery);
                      
           q.setParameter("idUsuario", idUsuario);
           q.setMaxResults(30);
          
           return q.getResultList();
       } catch (Exception e) {
           
           Logger.getLogger(PublicacionDAO.class.getName()).log(Level.SEVERE, "getPublicacionByUsuario", e);                   
           return null;

       }finally {
            if (em != null) {
                em.close();
            }
        }  
    }

    public Publicacion getPublicacionById(Integer idPub) {
        EntityManager em = null;
        String sQuery = "";
        try {
           sQuery = "SELECT p FROM Publicacion p "
                +" WHERE p.id = :id"                      
                +" ORDER BY p.fechaInicio DESC";                       
                        
           em = getEntityManager();           
           
           Query q = em.createQuery(sQuery);
                      
           q.setParameter("id", idPub);
           q.setMaxResults(1);
          
           return (Publicacion) q.getSingleResult();
       } catch (Exception e) {
           
           Logger.getLogger(PublicacionDAO.class.getName()).log(Level.SEVERE, "getPublicacionById", e);                   
           return null;
       }finally {
            if (em != null) {
                em.close();
            }
        }  
    }
    
    public Publicacion refeshPublicacion(Publicacion pub) {
        EntityManager em = null;
        
        try {
                        
           em = getEntityManager(); 
           em.refresh(pub);
           
           return pub;
       } catch (Exception e) {
           
           Logger.getLogger(PublicacionDAO.class.getName()).log(Level.SEVERE, "refeshPublicacion", e);                   
           return null;
       }finally {
            if (em != null) {
                em.close();
            }
        }  
    }

    
}
